package org.vectrics.recruiter.struts.admin.person;

import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.struts.action.ActionMessages;
import org.vectrics.cv.Studies;
import org.vectrics.cv.University;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

/**
 * @author pgoette
 * Action encargado de borrar un University
 */
public class DeleteUniversityAction extends VectricsStrutsAction {


	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		
		String forward = "success";
		Long idUniversity = actionData.getRequest().getLongParameter("universityId");
		
        if (idUniversity != null) {
        	// Busco la University segun el id.
        	University university = RecruitServiceLocator.getPersonService().findUniversity(idUniversity);
			// Recupero todas las university
        	List<Studies> studies = RecruitServiceLocator.getPersonService().findAllStudies();
			
        	for (Studies study : studies){
        		if (study.getUniversity()!=null) {
        			if (study.getUniversity().getId().equals(university.getId())) {
        				actionData.addError(ActionMessages.GLOBAL_MESSAGE, "error.delete.person.university");
        				return forward;
        			}        			
        		}
        	}
        	
        	RecruitServiceLocator.getPersonService().deleteUniversity(university);        		
		} 
		return forward;
	}

}
